define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'byd_shop_order/index' + location.search,
                    add_url: 'byd_shop_order/add',
                    edit_url: 'byd_shop_order/edit',
                    del_url: 'byd_shop_order/del',
                    multi_url: 'byd_shop_order/multi',
                    import_url: 'byd_shop_order/import',
                    table: 'byd_shop_order',
                }
            });

            var table = $("#table");

var opt = {
                    url: "byd_order_return/index?search=",
                    silent: true,
                    query:{
                        filter:'{"state":0}'
                    }
                };

            $(document).on('click', '.btn-state00', function () {
               opt.query.filter = '{"state":0}'; 
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state01', function () {
                opt.query.filter = '{"state":1}';           
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state02', function () {
                opt.query.filter = '{"state":2}';            
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state03', function () {
                opt.query.filter = '{"state":3}';            
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state04', function () {
                opt.query.filter = '{"state":4}';             
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state05', function () {
                opt.query.filter = '{"state":5}';             
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state06', function () {
                opt.query.filter = '{"state":6}';             
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state07', function () {
                opt.query.filter = '{"state":7}';            
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state09', function () {
                opt.query.filter = '{"state":9}';            
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state10', function () {
                opt.query.filter = '{"state":10}';
                table.bootstrapTable('refresh',opt);
            });
            $(document).on('click', '.btn-state13', function () {
                opt.query.filter = '{"state":13}';
                table.bootstrapTable('refresh',opt);
            });
            //
            // let time =setInterval(() => {
            //     $.ajax({
            //         type: 'POST',
            //         url: 'byd_shop_order/isHaveNewAfter',
            //         success: function (data) {
            //             if (data.code == 1) {
            //                 if (data.data > 0) {
            //                     // 提示音
            //                     // var audio = document.getElementById('reftips');
            //                     // audio.play();
            //                     // 提示框
            //                     Layer.alert('您有新的退换货订单，请及时处理', {
            //                         skin: 'layui-layer-molv' //样式类名  自定义样式
            //                         , closeBtn: 0    // 是否显示关闭按钮
            //                         , anim: 1 //动画类型
            //                         , btn: ['马上去处理', '我知道了'] //按钮
            //                         , icon: 6    // icon
            //                         , yes: function (index) {
            //                             handleTui('go');
            //
            //                             //audio.pause();
            //                             //audio.currentTime = 0;
            //                             layer.close(index);
            //                         }, btn2: function (index) {
            //                             handleTui('know');
            //                             //audio.pause();
            //                             //audio.currentTime = 0;
            //
            //                             layer.close(index);
            //                         }
            //                     });
            //                 }
            //             }
            //         }
            //     });
            // }, 1000*30);
            function handleTui(type) {
                $.ajax({
                    type: 'POST',
                    url: 'byd_shop_order/handleTui',
                    data: { type: type },
                    async: false,//同步
                    success: function (data) {
                        if (data.code == 1) {
                            if (data.data == 'go') {
                                // 点击菜单 "订单管理"，打开tab页
                                $('.sidebar-menu li a[addtabs=129]').click();
                            }
                        }
                    }
                });
            }
            // let times =setInterval(() => {
            //     $.ajax({
            //         type: 'POST',
            //         url: 'byd_shop_order/isHaveNewAfters',
            //         success: function (data) {
            //             if (data.code == 1) {
            //                 if (data.data > 0) {
            //                     // 提示音
            //                     // var audio = document.getElementById('reftips');
            //                     // audio.play();
            //                     // 提示框
            //                     Layer.alert('您有新的订单，请及时处理', {
            //                         skin: 'layui-layer-molv' //样式类名  自定义样式
            //                         , closeBtn: 0    // 是否显示关闭按钮
            //                         , anim: 1 //动画类型
            //                         , btn: ['马上去处理', '我知道了'] //按钮
            //                         , icon: 6    // icon
            //                         , yes: function (index) {
            //                             handleTui('go');
            //
            //                             //audio.pause();
            //                             //audio.currentTime = 0;
            //                             layer.close(index);
            //                         }, btn2: function (index) {
            //                             handleTui('know');
            //                             //audio.pause();
            //                             //audio.currentTime = 0;
            //
            //                             layer.close(index);
            //                         }
            //                     });
            //                 }
            //             }
            //         }
            //     });
            // }, 1000*30);
            function handleTui(type) {
                $.ajax({
                    type: 'POST',
                    url: 'byd_shop_order/handleTuis',
                    data: { type: type },
                    async: false,//同步
                    success: function (data) {
                        if (data.code == 1) {
                            if (data.data == 'go') {
                                // 点击菜单 "订单管理"，打开tab页
                                $('.sidebar-menu li a[addtabs=129]').click();
                            }
                        }
                    }
                });
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',escape: false,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'score', title: __('订单类型'), searchList: {"0":__('普通商品订单'),"1":__('积分商品订单')}, formatter: Table.api.formatter.status},

                        {field: 'orderid', title: __('Orderid'), operate: 'LIKE'},
                        {field: 'create_at', title: __('Create_at')},
                        {field: 'username', title: __('username')},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2'),"3":__('State 3'),"4":__('State 4'),"5":__('State 5'),"6":__('State 6'),"7":__('State 7'),"9":__('State 9'),"10":__('退货申请'),"11":__('退货成功'),"12":__('退货驳回'),"13":__('换货审核中'),"14":__('换货申请已同意'),"15":__('换货已驳回'),"16":__('报装中'),"17":__('已安装')}, formatter: Table.api.formatter.normal},
                        {field: 'good_info', title: __('商品信息'), operate: 'LIKE'},
                        {field: 'num', title: __('Num')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'addressxq', title: __('Addressxq'), operate: 'LIKE'},
                        /*
                        {field: 'specidsize', title: __('Specidsize'), operate: 'LIKE'},*/
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},

                        {field: 'content', title: __('备注'), operate: 'LIKE'},

                        {field: 'expressname', title: __('Express'), operate: 'LIKE'},
                        {field: 'expressorder', title: __('Expressorder'), operate: 'LIKE'},
                        {field: 'dzg', title: __('是否需要定制柜设计师'), searchList:{"0":__('不需要'),"1":__('需要')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {name: 'detail', text: '详情', title: '详情', icon: 'fa fa-list', classname: 'btn btn-xs btn-primary btn-dialog', url: 'byd_shop_order/detail'},
                                {name: 'shouhou', text: '售后', title: '售后', icon: 'fa fa-list', classname: 'btn btn-xs btn-primary btn-dialog', url: 'byd_shop_order/shouhou'},
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        },

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});